package com.fyzk.activity.lottery.jc.beidan;

import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.alibaba.fastjson.JSON;
import com.fyzk.activity.lottery.jc.football.JCInfo;
import com.tyjc.lottery.R;

import java.util.List;

/**
 * Created by fanxs11 on 2017/11/22.
 */

public class BeiDan_RSPF_PayAdapter extends BeiDanBasePayAdapter {

    private int leftBallColor;

    public BeiDan_RSPF_PayAdapter(Context context, List<BeiDanChildBean> infors, OnBeiDanPayClickListerner listerner) {
        super(context, infors, listerner);
        for(BeiDanChildBean info:infors){
            BeiDanChildBean.SPInfo spInfo_s = info.btnHGPojo.get(info.LETVS_3);
            BeiDanChildBean.SPInfo spInfo_p = info.btnHGPojo.get(info.LETVS_1);
            BeiDanChildBean.SPInfo spInfo_f = info.btnHGPojo.get(info.LETVS_0);
            spInfo_f.isSel = info.isFu;
            spInfo_p.isSel = info.isPing;
            spInfo_s.isSel = info.isSheng;
            info.btnHGPojo.put(info.LETVS_0,spInfo_f);
            info.btnHGPojo.put(info.LETVS_1,spInfo_p);
            info.btnHGPojo.put(info.LETVS_3,spInfo_s);
        }
    }

    @Override
    public View getView(int i, View view, ViewGroup viewGroup) {
        final BeiDanChildBean info = infors.get(i);
        ViewHolder holder = null;
        if (view == null) {
            holder = new ViewHolder();
            view = LayoutInflater.from(context).inflate(R.layout.adapter_beidan_rspf_pay, null);
            holder.zhu = view.findViewById(R.id.beidan_pay_list_item_zhu);
            holder.delete = view.findViewById(R.id.beidan_pay_list_item_delete);
            holder.home = view.findViewById(R.id.beidan_pay_list_item_home);
            holder.vs = view.findViewById(R.id.beidan_pay_list_item_vs);
            holder.away = view.findViewById(R.id.beidan_pay_list_item_away);
            holder.week_num = view.findViewById(R.id.beidan_pay_list_matchnum);
            holder.sheng_cb = view.findViewById(R.id.beidan_pay_list_sheng);
            holder.ping_cb = view.findViewById(R.id.beidan_pay_list_ping);
            holder.fu_cb = view.findViewById(R.id.beidan_pay_list_fu);
            holder.rq = view.findViewById(R.id.beidan_pay_list_rq);
            holder.dan = view.findViewById(R.id.beidan_pay_list_item_dan);
            holder.jz_ll_spf = view.findViewById(R.id.jz_ll_spf);
            holder.jz_stop_spf = view.findViewById(R.id.jz_stop_spf);
            holder.jz_ll_rspf = view.findViewById(R.id.jz_ll_rspf);
            holder.jz_stop_rspf = view.findViewById(R.id.jz_stop_rspf);
            view.setTag(holder);
        } else {
            holder = (ViewHolder) view.getTag();
        }

        holder.week_num.setText(info.getMatchNum()+"");
        holder.home.setText(info.getHomeTeam());
        holder.away.setText(info.getAwayTeam());

        int leftBall = 0;
        try {
            leftBall = Integer.parseInt(info.getHandicap());
        } catch (NumberFormatException e) {

        }
        if (leftBall > 0) {
            leftBallColor = context.getResources().getColor(R.color.color_FF4258);
        } else if (leftBall == 0) {
            leftBallColor = context.getResources().getColor(R.color.color_4EA5FF);
        } else {
            leftBallColor = context.getResources().getColor(R.color.color_47965a);
        }
        holder.rq.setBackgroundColor(leftBallColor);
        holder.rq.setText(info.getHandicap());

        BeiDanChildBean.SPInfo spInfo_s = info.btnHGPojo.get(info.LETVS_3);
        BeiDanChildBean.SPInfo spInfo_p = info.btnHGPojo.get(info.LETVS_1);
        BeiDanChildBean.SPInfo spInfo_f = info.btnHGPojo.get(info.LETVS_0);
        spInfo_f.isSel = info.isFu;
        spInfo_p.isSel = info.isPing;
        spInfo_s.isSel = info.isSheng;
        info.btnHGPojo.put(info.LETVS_0,spInfo_f);
        info.btnHGPojo.put(info.LETVS_1,spInfo_p);
        info.btnHGPojo.put(info.LETVS_3,spInfo_s);
        Log.e("fjl", JSON.toJSONString(info));
        Log.e("fjl",info.isFu+" isFu "+info.isPing+" isPing "+info.isSheng+" isSheng");
        holder.sheng_cb.setText("让胜\n"+info.getLetvs_3());
        holder.ping_cb.setText("让平\n"+info.getLetvs_1());
        holder.fu_cb.setText("让负\n"+info.getLetvs_0());

        holder.sheng_cb.setChecked(info.isSheng);
        holder.ping_cb.setChecked(info.isPing);
        holder.fu_cb.setChecked(info.isFu);
        jiSuanZhuShu();

        holder.sheng_cb.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                info.isSheng = !info.isSheng;
                if (info.isSheng) {
                    info.checkNum++;
                } else {
                    info.checkNum--;
                }

                BeiDanChildBean.SPInfo spInfo_s = info.btnHGPojo.get(info.VS_3);
                spInfo_s.isSel = info.isFu;
                jiSuanZhuShu();
            }
        });

        holder.ping_cb.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                info.isPing = !info.isPing;
                if (info.isPing) {
                    info.checkNum++;
                } else {
                    info.checkNum--;
                }
                BeiDanChildBean.SPInfo spInfo_p = info.btnHGPojo.get(info.VS_1);
                spInfo_p.isSel = info.isFu;
                jiSuanZhuShu();
            }
        });

        holder.fu_cb.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                info.isFu = !info.isFu;
                if (info.isFu) {
                    info.checkNum++;
                } else {
                    info.checkNum--;
                }
                BeiDanChildBean.SPInfo spInfo_f = info.btnHGPojo.get(info.VS_0);
                spInfo_f.isSel = info.isFu;
                jiSuanZhuShu();
            }
        });

        holder.delete.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                info.clearAll();
                infors.remove(info);
                notifyDataSetChanged();
                if (infors.size() == 0) {
                    jiSuanZhuShu();
                }
            }
        });

        return view;
    }

    @Override
    protected void jiSuanZhuShu() {

        if (listerner != null) {
            listerner.onItemClick();
        }
    }

    class ViewHolder {
        LinearLayout zhu;
        LinearLayout delete;
        LinearLayout jz_ll_spf;
        TextView jz_stop_spf;
        LinearLayout jz_ll_rspf;
        TextView jz_stop_rspf;
        TextView home;
        TextView week_num;
        TextView vs;
        TextView away;
        TextView rq;
        CheckBox sheng_cb;
        CheckBox ping_cb;
        CheckBox fu_cb;
        CheckBox dan;

    }
}
